package com.twoscreen;

import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONObject;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.twoscreen.test.Test;
import com.twoscreen.test.TestMethod;

@PersistenceCapable
public class DataProtectionRequest {
	@NotPersistent
	private static final String toEmail = "tojamessmith@gmail.com";
	
	@NotPersistent
	private static final String toName = "James Smith";
	
	@NotPersistent
	private static PersistenceManager pm = PMF.get().getPersistenceManager();
	
	@NotPersistent
	private static final Logger log = Logger.getLogger("com.twoscreen.DataProtectionRequest.java");
	
	@NotPersistent
	private static final String formEmailAddressPropertyName = "formEmailAddress";
	
	@NotPersistent
	private static DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.LONG);
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String formEmailAddress;
	
	@Persistent
	private String userID;
	
	@Persistent
	private Date timestamp;
	
	/**
	 * Creates a new data protection request and triggers an email
	 * to be sent to the site administrator
	 * @param formEmailAddress The email address submitted on the form
	 * @param user The Google User object of the user submitting the form
	 */
	public DataProtectionRequest(String formEmailAddress, User user) {
		this.formEmailAddress = formEmailAddress;
		this.userID = user.getUserId();
		this.timestamp = new Date();
		pm.makePersistent(this);
		log.info("DPA request added: email: " + formEmailAddress +
				" User ID: " + user.getUserId() 
				);
	}

	public static String getFormemailaddresspropertyname() {
		return formEmailAddressPropertyName;
	}

	public Key getKey() {
		return key;
	}

	public String getFormEmailAddress() {
		return formEmailAddress;
	}

	public String getUserID() {
		return userID;
	}

	public Date getTimestamp() {
		return timestamp;
	}
	
	/**
	 * Runs all the tests on the class
	 * @return Test results JSON
	 */
	public static JSONObject test() {
		Test test = new Test("DataProtectionRequest");
		test.addMethod(testConstructor());
		return test.getJSONObject();
	}
	
	/**
	 * Tests the generateId method
	 * @return Test results JSON
	 */
	private static JSONObject testConstructor() {
		TestMethod tm = new TestMethod("testConstructor");
		
		//Test base
		String formEmail = "my1@test.email";
		User user = new User("user@email.com", "google", "123456");
		DataProtectionRequest dpr = new DataProtectionRequest(formEmail, user);
		Key key = dpr.getKey();
    	
		//Test 1
		Boolean test1 = false;
    	String testStack1 = "";
    	try {
    		DataProtectionRequest dprTest = pm.getObjectById(DataProtectionRequest.class, key.getId());
    		test1 = (dprTest != null);
    	} catch (Exception e) {
    		testStack1 = e.getMessage();
    	}
		tm.addTest("Entry exists", test1, testStack1);
		
		//Test 2
		Boolean test2 = false;
    	String testStack2 = "";
    	try {
    		DataProtectionRequest dprTest = pm.getObjectById(DataProtectionRequest.class, key.getId());
    		String email = dprTest.getFormEmailAddress();
    		test2 = (email.equals(formEmail));
    	} catch (Exception e) {
    		testStack2 = e.getMessage();
    	}
		tm.addTest("Form email correct", test2, testStack2);
		
		//Remove the test object
		pm.deletePersistent(dpr);
		
    	return tm.getJSONObject();
    }

}
